#include "friendpage.h"
#include "ui_friendpage.h"
#include <QScrollArea>
#include <QTimer>
#include <QDebug>
#include <QToolButton>
#include <networkcontroller.h>
#include "userchatmode.h"
FriendPage::FriendPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FriendPage)
{
    ui->setupUi(this);
    connect(NetWorkController::getInstance(),&NetWorkController::sendFriendInfo,this,&FriendPage::recvFriendInfo);






}


void FriendPage::recvFriendInfo(QJsonArray jobj)
{
    //            QString name = friendObj["name"].toString();
    //            QString age = friendObj["age"].toString();
    //            QString birthday = friendObj["birthday"].toString();
    //            QString headPortrait = friendObj["headPortrait"].toString();
    //            QString sex = friendObj["sex"].toString();
    //            QString userid = friendObj["userid"].toString();
    for (const QJsonValue& friendValue : jobj) {
        QJsonObject friendObj = friendValue.toObject();
        QString username=friendObj["name"].toString();
        QString uid=friendObj["userid"].toString();
        qDebug()<<"username:"<<username<<"\nuid:"<<uid;
        FriendMode *friendMode = new FriendMode(this,username,uid);
        connect(friendMode,&FriendMode::sendRemove,[=](){
            int index=frdMode.indexOf(friendMode);
            qDebug()<<index<<"btn";
            ui->layout->removeWidget(frdMode[index]);
            delete friendMode;
            frdMode.removeAt(index);
            ui->layout->update();
        });

        ui->layout->addWidget(friendMode);
        frdMode.append(friendMode);
    }

    ui->layout->addStretch();
    update();
}

FriendPage::~FriendPage()
{
    delete ui;

}

const QVector<FriendMode *> &FriendPage::getFrdMode() const
{
    return frdMode;
}

void FriendPage::setFrdMode(const QVector<FriendMode *> &newFrdMode)
{
    frdMode = newFrdMode;
}


